package com.schooldate.model.service.impl;

import com.schooldate.base.BaseSupportImpl;
import com.schooldate.model.entity.School;
import com.schooldate.model.service.SchoolService;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by etund on 2015/3/15 0015.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class SchoolServiceImpl extends BaseSupportImpl<School> implements SchoolService {

    @Override
    public List<School> getSchoolByPlace(String nearPlaceName) {
       String sid ;
        list = this.getSession().createCriteria(School.class)
                                    .add(Restrictions.eq("name",nearPlaceName))
                                    .list();
        if (list.size()>0){
            School school = list.get(0);
            sid = school.getSid();
            list = this.getSession().createCriteria(School.class)
                    .add(Restrictions.like("sid",sid+"___"))
                    .list();
        }
        return list;
    }


    @Override
    public List<School> getBySchoolNames(String[] names) {
        String sql = "from School s where ";
        if (names.length>0){
            sql += " s.name like '";
            for (String name:names){
                if (!name.trim().equals("")){
                    sql += "%"+name;
                }
            }
            sql += "%'";
            System.out.println(sql+"===============================");
        }
        System.out.println(sql+"==========================");
        return getSession().createQuery(sql).list();
    }

    @Override
    public School getBySchoolName(String schoolId) {
        list = this.getSession().createCriteria(School.class)
                .add(Restrictions.eq("name",schoolId))
                .list();
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
